package com.gudaomai.wuye.web;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.entity.Community;
import com.gudaomai.wuye.entity.CommunityCreatTreeInput;
import com.gudaomai.wuye.entity.CommunityInput;
import com.gudaomai.wuye.entity.RetEntity;
import com.gudaomai.wuye.service.ICommunityServ;
import com.gudaomai.wuye.service.IHouseServ;
import com.gudaomai.wuye.service.ILayoutServ;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-08-18
 */
@Controller
@RequestMapping("/wuye/community")
public class CommunityCtrl extends BaseController {
	
	@Autowired
	ICommunityServ communityService;
	
	@Autowired
	IHouseServ houseServ;
	
	@Autowired
	ILayoutServ layoutServ;
	
	/** 
	 * Save an new community
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. Check if the community with the same properties does not exist in the DB.
	 * 4. Save the inputed values.
	 * 
	 *@param  
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The community settings page, click the add button, add community")
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Community addCommunity(@RequestBody CommunityInput communityInput) throws CommonException {
			
		    //Delete all data in all database tables. 
		    communityService.resetDB();
		    
			// Create new community entity
			Community communityEntity = new Community();
			
			// copy all user inputs to the new community entity
			BeanUtils.copyProperties(communityInput, communityEntity);
			
			// Insert the community entity into the database
			communityService.insert(communityEntity);
			
		
		return communityEntity;
	}
	
	/** 
	 * Save an new community
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. Check if the community with the same properties does not exist in the DB.
	 * 4. Save the inputed values.
	 * 
	 *@param  
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("生成房屋树结构")
	@RequestMapping(value = "/creatTree", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public RetEntity communityCreatTree(@RequestBody CommunityCreatTreeInput communityCreatTreeInput) throws CommonException {
		RetEntity entity = new RetEntity();
		//houseServ.insertBatch(entityList);
		//layoutServ
		
		return entity;
	}
	
	
}
